/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dtd.to.xml;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author martin
 * @author Mato Majdis
 */
public class Main {
    public static void main(String[] args) {

        /*
          OSETRUJUCA PODMIENKA NA POCET ARGUMENTOV
          Ak nebol zadany na vstupe ziadny subor tak sa program ukonci
        */
        if (args.length == 0) {
            System.err.println("No XML file to parse. Please enter XML file as an argument");
            return;
        }
        
        DoctypeParser dp = new DoctypeParser(args[0]);
        String dtdPath = dp.getDTDLink();  
        DTDParser dtd = new DTDParser(dtdPath);
        try {
            dtd.parseDTD();
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }                   
        try{
            XSDConverter name = new XSDConverter();
            FileWriter fw = new FileWriter("output.xsd");            
            PrintWriter out = new PrintWriter(fw);            
            out.println(name.convert(dtd).toString());
            out.close();
            fw.close();
	}catch(IOException e) {
            System.err.println(e.getMessage());	
	} 
        
        DoctypeRemover remover = new DoctypeRemover(args[0]);
        remover.setNS();
        remover.transformDoc();
        
        if(dtdPath.contains("temporary.dtd")){
            File tmp = new File(dtdPath);
            if(tmp.exists())
                tmp.delete();
        }
        
    }
}
